﻿#if DEBUG_SHOW

using System.Collections.Generic;
using TMPro;
using TrueSync;
using UnityEngine;
using UnityEngine.UI;

namespace HEFramework
{
    public static class DebugUISystem
    {
        [EntitySystem]
        public class DebugUIAwakeSystem : AwakeSystem<DebugUI>
        {
            protected override void Awake(DebugUI _self)
            {
                _self.Open();
            }
        }

        [EntitySystem]
        public class DebugUIDestroySystem : DestroySystem<DebugUI>
        {
            protected override void Destroy(DebugUI _self)
            {
                _self.Close();
            }
        }

        public static void Open(this DebugUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.Debug, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.ClosePanel();
                _self.Update(true);
            });
        }

        public static void Bind(this DebugUI _self)
        {
            _self.UIWindowBehaviour.GetButton("Button_Open").RegisterDownEvent(_self.ShowPanel);
            _self.UIWindowBehaviour.GetButton("Button_Close").RegisterDownEvent(_self.ClosePanel);
            var contain = _self.UIWindowBehaviour.GetTransform("Transform_Content");
            for (int i = 0; i < contain.childCount; i++)
            {
                var transform = contain.GetChild(i);
                _self.Register(transform, i);
            }
        }

        private static void Update(this DebugUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }
        }

        public static void Close(this DebugUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }

        private static void ShowPanel(this DebugUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Open").GetGameObject().SetActive(false);
            _self.UIWindowBehaviour.GetButton("Button_Close").GetGameObject().SetActive(true);
            _self.UIWindowBehaviour.GetTransform("Transform_Panel").gameObject.SetActive(true);
            _self.UIWindowBehaviour.GetTransform("Transform_Panel").GetComponent<ScrollRect>().verticalScrollbar.value = 1;
        }

        private static void ClosePanel(this DebugUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Open").GetGameObject().SetActive(true);
            _self.UIWindowBehaviour.GetButton("Button_Close").GetGameObject().SetActive(false);
            _self.UIWindowBehaviour.GetTransform("Transform_Panel").gameObject.SetActive(false);
        }

        private static void Register(this DebugUI _self, Transform _transform, int _index)
        {
            var dc = _self.GetSceneChild<DataTableComponent>();
            if (dc == null)
            {
                return;
            }

            if (_index >= _self.Infos.Length)
            {
                return;
            }

            var info = _self.Infos[_index];
            var attribute_table = dc.LoadDataTable<DRAttribute>();
            var text = _transform.GetChild(0).GetComponent<TMP_Text>();
            var dropdown = _transform.GetChild(1).GetComponent<TMP_Dropdown>();
            var input = _transform.GetChild(2).GetComponent<TMP_InputField>();
            var button = _transform.GetChild(3).GetComponent<Button>();
            dropdown.ClearOptions();

            text.text = info.Name;
            if (info.DropdownOpen == 0)
            {
                dropdown.gameObject.SetActive(false);
            }

            if (info.InputOpen == 0)
            {
                input.gameObject.SetActive(false);
            }

            switch (_index)
            {
                case (int)DebugUI.eDebug.Debug_Reward:
                {
                    var reward = _self.GetSceneChild<Reward>();
                    var datas = dc.LoadDataTable<DRReward>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = datas[i].Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        int value;
                        int.TryParse(input.text, out value);
                        reward.ChangeTeamReward(id, value);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Hero:
                {
                    var team = _self.GetSceneChild<Team>();
                    var datas = dc.LoadDataTable<DRHero>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = attribute_table.GetData(datas[i].ID).Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        team.AddHeroRandomPos(id);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Monster:
                {
                    var factory = _self.GetSceneChild<Factory>();
                    var datas = dc.LoadDataTable<DRMonster>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = attribute_table.GetData(datas[i].ID).Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        int teamID = 1;
                        var e = factory.AddMonster(id, teamID, factory.GetRandomMonsterGeneratePos());
                        e.GetChild<AlertComponent>().SetType((int)eAlertType.Composite);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Monster_Near:
                {
                    var factory = _self.GetSceneChild<Factory>();
                    var datas = dc.LoadDataTable<DRMonster>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = attribute_table.GetData(datas[i].ID).Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        int teamID = 1;
                        var e = factory.AddMonster(id, teamID, new TSVector(3, 3, 0));
                        e.GetChild<AlertComponent>().SetType((int)eAlertType.Composite);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Monset_Npc:
                {
                    var factory = _self.GetSceneChild<Factory>();
                    button.onClick.AddListener(() =>
                    {
                        int teamID = 1;
                        var e = factory.AddMonster(9999, teamID, new TSVector(3, 3, 0));
                        e.GetChild<AlertComponent>().SetType((int)eAlertType.Composite);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Legacy:
                {
                    var gameUI = _self.GetSceneChild<GameUI>();
                    var legacy = _self.GetSceneChild<Legacy>();
                    var datas = dc.LoadDataTable<DRLegacy>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = datas[i].Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        legacy.FindLegacy(id);
                        legacy.AddFindLegacy(id);
                        gameUI.Update(true);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_LegacyCount:
                {
                    var legacyLottery = _self.GetSceneChild<LegacyLottery>();
                    button.onClick.AddListener(() =>
                    {
                        int value;
                        int.TryParse(input.text, out value);
                        legacyLottery.LotteryCount += value;
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Element:
                {
                    var element = _self.GetSceneChild<Element>();
                    var datas = dc.LoadDataTable<DRElement>().GetAllData();
                    for (int i = 0; i < datas.Length; i++)
                    {
                        string name = datas[i].Name;
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int id = datas[dropdown.value].ID;
                        int value;
                        int.TryParse(input.text, out value);
                        if (value != 0)
                        {
                            element.ChangeElement(id, value);
                        }
                        else
                        {
                            element.ChangeElement(id);
                        }
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Scene:
                {
                    long[] scenes = new long[]
                    {
                        (long)eSceneType.Test,
                        (long)eSceneType.Game,
                    };
                    string[] names = new string[]
                    {
                        eSceneType.Test.ToString(),
                        eSceneType.Game.ToString(),
                    };
                    for (int i = 0; i < names.Length; i++)
                    {
                        string name = names[i];
                        dropdown.options.Add(new TMP_Dropdown.OptionData(name));
                    }

                    button.onClick.AddListener(() =>
                    {
                        int value;
                        int.TryParse(input.text, out value);
                        if (value == 0)
                        {
                            value = 100;
                        }

                        eSceneType scene = (eSceneType)scenes[dropdown.value];
                        World.Instance.DestroyScene(_self.IScene);
                        World.Instance.CreateScene(scene, value);
                    });
                }
                    break;
                case (int)DebugUI.eDebug.Debug_Max_LV:
                {
                    var reward = _self.GetSceneChild<Reward>();
                    button.onClick.AddListener(() => { reward.ChangeTeamReward((int)eReward.LV, 50); });
                }

                    break;
                case (int)DebugUI.eDebug.Debug_Max_RANK:
                {
                    var reward = _self.GetSceneChild<Reward>();
                    button.onClick.AddListener(() => { reward.ChangeTeamReward((int)eReward.RANK, 5); });
                }

                    break;
            }

            dropdown.RefreshShownValue();
        }
    }
}
#endif